package com.niit.blog.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.niit.blog.common.MyResult;
import com.niit.blog.entity.Dto.CommentControDto;
import com.niit.blog.entity.domain.Blog;
import com.niit.blog.entity.domain.TbBlogComment;
import com.niit.blog.entity.page.PageReq;
import com.niit.blog.entity.page.PageReqreq;
import com.niit.blog.entity.page.PageResponse;
import com.niit.blog.entity.req.CommentReq;
import com.niit.blog.entity.req.GetShareCommentReq;
import com.niit.blog.entity.req.RemoveShareCommentReq;
import com.niit.blog.entity.req.SaveBlogCommentReplyReq;
import com.niit.blog.entity.sensitive.WordFilter;
import com.niit.blog.service.BlogService;
import com.niit.blog.service.TbBlogCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/comment")

public class CommentController {
    @Resource
    private BlogService blogService;
    @Resource
    private TbBlogCommentService shareCommentReplyService;
    @Resource
    private WordFilter wordFilter;


//    /**
//     * 发布内容
//     */
//    @PostMapping(value = "/save")
//    public MyResult<Integer> save(@RequestBody SaveBlogCommentReplyReq req) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("发布内容入参{}", JSON.toJSONString(req));
//            }
//            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
//            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "类型不能为空！");
//            Preconditions.checkArgument(Objects.nonNull(req.getBlogId()), "博客ID不能为空！");
//            Preconditions.checkArgument(Objects.nonNull(req.getTargetId()), "目标ID不能为空！");
//            Blog blog = blogService.getBlogById(req.getBlogId());
//
//            Preconditions.checkArgument((Objects.nonNull(req.getContent())), "内容不能为空！");
//            Integer result = shareCommentReplyService.saveBlogComment(null);
//            if (log.isInfoEnabled()) {
//                log.info("发布内容{}", JSON.toJSONString(result));
//            }
//            return MyResult.success(result);
//        } catch (IllegalArgumentException e) {
//            log.error("参数异常！错误原因{}", e.getMessage(), e);
//            return MyResult.fail(e.getMessage());
//        } catch (Exception e) {
//            log.error("发布内容异常！错误原因{}", e.getMessage(), e);
//            return MyResult.fail("发布内容异常！");
//        }
//    }


    /**
     * 删除评论内容
     */
    @PostMapping(value = "/remove")
    public MyResult<Boolean> remove(@RequestBody RemoveShareCommentReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("删除鸡圈评论内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "类型不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "内容ID不能为空！");
            Boolean result = shareCommentReplyService.removeComment(req);
            if (log.isInfoEnabled()) {
                log.info("删除鸡圈评论内容{}", JSON.toJSONString(result));
            }
            return MyResult.success(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return MyResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除鸡圈评论内容异常！错误原因{}", e.getMessage(), e);
            return MyResult.fail("删除鸡圈评论内容异常！");
        }
    }

    /**
     * 查询该动态下的评论
     */
//    @PostMapping(value = "/list")
//    public MyResult<List<ShareCommentReplyVO>> list(@RequestBody GetShareCommentReq req) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("获取鸡圈评论内容入参{}", JSON.toJSONString(req));
//            }
//            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
//            Preconditions.checkArgument(Objects.nonNull(req.getId()), "内容ID不能为空！");
//            List<ShareCommentReplyVO> result = shareCommentReplyService.listComment(req);
//            if (log.isInfoEnabled()) {
//                log.info("获取鸡圈评论内容{}", JSON.toJSONString(result));
//            }
//            return MyResult.success(result);
//        } catch (IllegalArgumentException e) {
//            log.error("参数异常！错误原因{}", e.getMessage(), e);
//            return MyResult.fail(e.getMessage());
//        } catch (Exception e) {
//            log.error("获取鸡圈评论内容异常！错误原因{}", e.getMessage(), e);
//            return MyResult.fail("获取鸡圈评论内容异常！");
//        }
//    }
    /**
     * 后端管理
     *
     */

//    @RequestParam(required = false,defaultValue = "1") Integer pageNo,
//    @RequestParam(required = false,defaultValue = "10") Integer pageSize
    @PostMapping("/selectByPage")
    public PageResponse selectByPage(@RequestBody(required = false) PageReq pageReq
                                    ){
        PageReqreq pageReqreq = new PageReqreq();
        pageReqreq.setBlogId(pageReq.getBlogId());
        pageReqreq.setIsDeleted(pageReq.getIsDeleted());
        pageReqreq.setReplyId(pageReq.getReplyId());
        pageReqreq.setReplyType(pageReq.getReplyType());
        pageReqreq.setReplyUser(pageReq.getReplyUser());
        pageReqreq.setToId(pageReqreq.getToId());

        Integer pageNo = pageReq.getCurrentPage();
        Integer pageSize = pageReq.getPageSize();





        PageInfo<CommentControDto> pageInfo = shareCommentReplyService.selectByPage(pageReqreq,pageNo,pageSize);




        return PageResponse.suc(pageInfo);
    }
//    @GetMapping("/selectByPage")
//    public PageResponse selectByPage(@RequestParam(required = false) Integer blogId,
//                                     @RequestParam(required = false) String replyType,
//                                     @RequestParam(required = false) Integer toId,
//                                     @RequestParam(required = false) Integer replyId,
//                                     @RequestParam(required = false) String replyUser,
//                                     @RequestParam(required = false) String replyAuthor,
//                                     @RequestParam(required = false) Integer isDeleted,
//                                     @RequestParam(required = false,defaultValue = "1") Integer pageNo,
//                                     @RequestParam(required = false,defaultValue = "10") Integer pageSize){
//
//        PageReq pageReq = new PageReq();
//        pageReq.setBlogId(blogId);
//        pageReq.setReplyId(replyId);
//        pageReq.setReplyType(replyType);
//        pageReq.setIsDeleted(isDeleted);
//        pageReq.setReplyAuthor(replyAuthor);
//        pageReq.setToId(toId);
//        pageReq.setReplyUser(replyUser);
//        PageInfo<CommentControDto> pageInfo = shareCommentReplyService.selectByPage(pageReq,pageNo,pageSize);
//
//
//
//
//        return PageResponse.suc(pageInfo);
//    }

//新增评论
    @PostMapping("/addComment")
    public MyResult<Integer>  addComment(@RequestBody CommentReq commentReq){


        String replace = wordFilter.replace(commentReq.getContent(), '*');
        log.info("shuju:{}",commentReq);
        commentReq.setContent(replace);

        TbBlogComment tbBlogComment = shareCommentReplyService.prepareData(commentReq);
        log.info("tbBlogComment转换之后的数据为:{}",tbBlogComment);

        Integer integer = shareCommentReplyService.saveBlogComment(tbBlogComment);
        return MyResult.success(integer);
    }
//删除评论


}
